package Demo01;

import java.util.HashSet;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int[] nums = {7,2,5,10,8};
        System.out.println(splitArray(nums, 2));
    }

    public static int splitArray(int[] nums, int k) {
        //范围: 1~sum
        long sum = 0;
        for (int num : nums) {
            sum += num;
        }
        long l = 0;
        long r = sum;
        long ans = 0;
        while(l<=r) {
            long m = l + ((r-l)>>1);
            long res = f(nums, m);
            if(res <= k) {
                r = m - 1;
                ans = m;
            }else {
                l = m + 1;
            }
        }
        return (int)ans;
    }
    private static long f(int[] arr, long cmp) {
        int k = 1;
        long sum = 0;
        for (int i = 0; i < arr.length; i++) {
            if(cmp < arr[i]) {
                return Integer.MAX_VALUE;
            }
            if(sum + arr[i] > cmp) {
                k++;
                sum = arr[i];
            }else {
                sum += arr[i];
            }
        }
        return k;
    }

    public int minEatingSpeed(int[] piles, int h) {
        //确定速度范围 1~max
        int min = 1;
        int max = 0;
        for (int i : piles) {
            max = (i > max) ? i : max;
        }
        int res = 0;
        while(min <= max) {
            int mid = min + ((max - min) >> 1);
            if(getHours(piles, mid) > h) {
                //超时
                //向右提高速度
                min = mid + 1;
            }else {
                //不超时
                //向左继续查看最小
                res = mid;
                max = mid - 1;
            }
        }

        return res;
    }
    public static long getHours(int[] arr, int speed) {
        long sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += (arr[i]+speed-1)/speed;
        }

        return sum;
    }
    public int canCompleteCircuit1(int[] gas, int[] cost) {
        int n = gas.length;
        int index = -1;
        int sum1 = 0;
        int sum2 = 0;
        for (int i = 0; i < n; i++) {
            sum1 += gas[i];
            sum2 += cost[i];

        }
        for (int i = 0; i < n; i++) {
            if(gas[i] >= cost[i]) {
                index = i;
                break;
            }
        }
        if(sum1<sum2) {
            return -1;
        }

        return index;
    }
    public static void main2(String[] args) {
        String string = "abcabcbb";
        System.out.println(lengthOfLongestSubstring(string));
    }
    public static int lengthOfLongestSubstring(String s) {
        HashSet<Character> set = new HashSet<>();
        int count = Integer.MAX_VALUE;
        for (int r = 0, l = 0; r < s.length(); r++) {
            char tmp = s.charAt(r);
            if(!set.contains(tmp)) {
                set.add(tmp);
            } else {
                count = count>(r-l+1)?(r-l+1):count;
                l = r;
            }
        }
        return count;
    }
    public static void main1(String[] args) {
        Scanner scanner = new Scanner(System.in);
        //数组长度
        int l = scanner.nextInt();
        int[] arr = new int[l];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = scanner.nextInt();
        }
        HashSet<Integer> set = new HashSet<>();
        //set清零后要将reSet数据倒进去
        HashSet<Integer> reSet = new HashSet<>();
        for (int i = 0; i < arr.length; i++) {
            set.add(arr[i]);
            reSet.add(arr[i]);
        }
        //set存放不同的数
        //再申请一个数组存放最短, minl进行计数,  比较
        for (int i = 0; i < arr.length; i++) {

        }
    }
}
